Course Introduction
Old Dominion University
Sequential measurements or values of a single entity over time.
Why study time series data?
“Predict” outcomes of a time series in future (unobserved) periods.
\[Y_{t} = f(Y_{t-1}, X_{t}, X_{t-1}, \tau_t, C_t, S_t)\] where:
Define Problem
What am I trying to solve?
Gather Data
FRED, WRDS, Etc.
Exploratory Data Analysis (EDA)
Plot, Plot, Plot
Choose & Fit a Model
\(Y_{t} = f(Y_{t-1}, X_{t}, X_{t-1}, \tau_t, C_t, S_t)\)
Evaluate, Forecast
dplyrtidyverseggplot2group_by(), summarise(), left_join()paste(), paste0(), substr(), grepl(), gsub(), regexpr(), strsplit(), unlist()for()if(), elseifelse()fixestscales::alpha()data.table::fread()readRDS(), saveRDS()lubridate1tsibbleforecastts()2duplicated() date urate_t
1 1948-01-01 3.4
2 1948-02-01 3.8
3 1948-03-01 4.0
4 1948-04-01 3.9
5 1948-05-01 3.5
6 1948-06-01 3.6
urate <- urate %>%
mutate(date = ymd(date))
urate %>%
ggplot(aes(x = date, y = urate_t)) +
geom_point() +
labs(
title = "Monthly Unemployment Rate",
x = "Month",
y = "Unemployment Rate"
) +
theme_minimal() + # Simpler background theme with x and y axis
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)ggplot(urate, aes(x = date, y = urate_t)) +
geom_line(color = "dodgerblue") +
labs(
title = "Monthly Unemployment Rate",
x = "Time",
y = "Unemployment Rate"
) +
theme_minimal() + # Simpler background theme with x and y axis
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
) date urate_t
1 1948-01-01 3.4
2 1948-02-01 3.8
3 1948-03-01 4.0
4 1948-04-01 3.9
5 1948-05-01 3.5
6 1948-06-01 3.6
date urate_t urate_tm1 urate_tp1
1 1948-01-01 3.4 NA 3.8
2 1948-02-01 3.8 3.4 4.0
3 1948-03-01 4.0 3.8 3.9
4 1948-04-01 3.9 4.0 3.5
5 1948-05-01 3.5 3.9 3.6
6 1948-06-01 3.6 3.5 3.6
# A tibble: 9 × 3
state year var
<chr> <int> <dbl>
1 NY 2010 -0.456
2 NY 2011 0.935
3 NY 2012 -0.00928
4 VA 2010 0.0632
5 VA 2011 1.61
6 VA 2012 0.598
7 CA 2010 -0.924
8 CA 2011 -0.581
9 CA 2012 1.76
# A tibble: 9 × 4
state year var var_lag1
<chr> <int> <dbl> <dbl>
1 NY 2010 -0.456 NA
2 NY 2011 0.935 -0.456
3 NY 2012 -0.00928 0.935
4 VA 2010 0.0632 NA
5 VA 2011 1.61 0.0632
6 VA 2012 0.598 1.61
7 CA 2010 -0.924 NA
8 CA 2011 -0.581 -0.924
9 CA 2012 1.76 -0.581
ggplot(df, aes(x = as.factor(year), y = var)) +
geom_point() +
labs(
title = "",
x = "Year",
y = "Var Value" # Modified y-axis label
) +
theme_minimal() + # Simpler background theme with x and y axis
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)library(ggplot2)
ggplot(df, aes(x = as.factor(year), y = var, group = state, color = state)) +
geom_point() +
scale_color_brewer(name = "State", palette = "Set1") + # Different set of colors
labs(
title = "",
x = "Year",
y = "Var Value" # Modified y-axis label
) +
theme_minimal() + # Simpler background theme with x and y axis
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)ggplot(df, aes(x = as.factor(year), y = var, group = state, color = state)) +
geom_line() +
scale_color_manual(name = "State", values = c("NY" = "#FF9999", "VA" = "#99CCFF", "CA" = "#99FF99")) + # Pastel color scheme
labs(
title = "",
x = "Year",
y = "Var"
) +
theme_minimal() + # Simpler background theme with x and y axis
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)df %>%
filter(state == "NY") %>%
ggplot(aes(x = as.factor(year), y = var, color = state)) +
geom_line(group = 1) +
scale_color_manual(values = c("NY" = "#FF9999")) + # Soft pink color for NY
labs(
title = "",
x = "Year",
y = "Var"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)df %>%
ggplot(aes(x = as.factor(year), y = var, color = state, group = state)) +
geom_line() +
scale_color_brewer(palette = "Pastel1") + # Using the "Pastel1" palette for milder colors
labs(
title = "",
x = "Year",
y = "Var"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 0, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)df %>%
ggplot(aes(x = as.factor(year), y = var, color = state)) +
geom_line(group = 1) +
scale_color_brewer(palette = "Pastel1") + # Using the "Pastel1" palette for milder colors
labs(
title = "",
x = "Year",
y = "Var"
) +
facet_wrap(~ state, scales = "free_y") + # Separate graphs based on state
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5), # Centering title
axis.text.x = element_text(angle = 45, hjust = 1, color = "black"),
axis.text.y = element_text(color = "black")
)ECON 707/807: Econometrics II